#ifndef SELECTSTOREDEXPRESSIONDLG_H
#define SELECTSTOREDEXPRESSIONDLG_H

#include <map>
#include <wx/sizer.h>
#include <wx/button.h>
#include "scrollingdialog.h"
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/timer.h>
#include <wx/listbox.h>

class SelectStoredExpressionDlg: public wxScrollingDialog {
  public:
    SelectStoredExpressionDlg( wxWindow* parent, const wxString& startingExpresion = wxEmptyString );
    virtual ~SelectStoredExpressionDlg();
    wxString GetExpression() {
      return m_Expression;
    }
  private:
    wxString m_Expression;
    wxButton* Button4;
    wxButton* Button1;
    wxButton* Button2;
    wxButton* Button3;
    wxStaticText* StaticText1;
    wxTimer Timer1;
    wxListBox* m_Expressions;
    wxTextCtrl* m_Filter;
    static const long ID_LISTBOX1;
    static const long ID_STATICTEXT1;
    static const long ID_TEXTCTRL1;
    static const long ID_BUTTON1;
    static const long ID_BUTTON2;
    static const long ID_BUTTON3;
    static const long ID_BUTTON4;
    static const long ID_TIMER1;
    void OnOkClick( wxCommandEvent& event );
    void OnCancelClick( wxCommandEvent& event );
    void OnButton1Click( wxCommandEvent& event );
    void Onm_ExpressionsSelect( wxCommandEvent& event );
    void OnButton2Click( wxCommandEvent& event );
    void OnButton3Click( wxCommandEvent& event );
    void Onm_FilterText( wxCommandEvent& event );
    void Onm_FilterTextEnter( wxCommandEvent& event );
    void OnTimer1Trigger( wxTimerEvent& event );
    void OnButton4Click( wxCommandEvent& event );
    void Onm_ExpressionsDClick( wxCommandEvent& event );
    void BuildContent( wxWindow* parent );
    void ReadExpressions();
    void StoreExpressions();
    void RecreateExpressionsList( const wxString& selectionHint = wxEmptyString );
    typedef std::map< wxString, wxString > CacheT;
    CacheT m_Cache;
    bool m_CacheChanged;

    class ListData: public wxClientData {
      public:
        inline ListData( const CacheT::iterator& i ): m_Iterator( i ) {}
        inline const CacheT::iterator& GetIterator() {
          return m_Iterator;
        }
        inline const wxString& GetKey() {
          return m_Iterator->first;
        }
        inline const wxString& GetValue() {
          return m_Iterator->second;
        }
      private:
        CacheT::iterator m_Iterator;
    };
    ListData* GetSelection();
    void AddingExpression( const wxString& defaultName, const wxString& defaultValue );
    void FilterUpdated();
    void StoreExpressionsQuery();
    static wxString GetListName( const wxString& name, const wxString& expr );
    DECLARE_EVENT_TABLE()
};

#endif
